package com.example.wanbaqian.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.wanbaqian.R;
import com.example.wanbaqian.adapter.PayMoneyAdapter;
import com.example.wanbaqian.base.BaseActivity;
import com.example.wanbaqian.config.CommonValue;
import com.example.wanbaqian.base.HttpCallback;
import com.example.wanbaqian.config.HttpURL;
import com.example.wanbaqian.bean.BackMoneyDetailsBean;
import com.example.wanbaqian.bean.CouponInfo;
import com.example.wanbaqian.bean.Info;
import com.example.wanbaqian.utils.GetJsonUtils;
import com.example.wanbaqian.utils.HttpUtils;
import com.example.wanbaqian.utils.JsonUtils;
import com.example.wanbaqian.utils.MobileSecurePayer;
import com.example.wanbaqian.utils.SPUtils;
import com.example.wanbaqian.utils.TimeUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/4/13.
 * 还款详情--13521125332
 */
public class BackMoneyOneActivity extends BaseActivity implements HttpCallback {
    private static final String TAG = "BackMoneyOneActivity";
    @BindView(R.id.btn_ok)
    Button btn_ok;
    @BindView(R.id.rl_result)
    LinearLayout rl_result;

    @BindView(R.id.shouldrepaymoney)
    TextView mShouldrepaymoney;

    @BindView(R.id.txt_money)
    TextView mMoney;

    @BindView(R.id.txt_latefee)
    TextView mLatefee;

    @BindView(R.id.orderid)
    TextView mOrder;

    @BindView(R.id.txt1)
    TextView mTxt1;

    @BindView(R.id.txt2)
    TextView mTxt2;
    @BindView(R.id.txt3)
    TextView mTxt3;

    @BindView(R.id.txt4)
    TextView mTxt4;

    @BindView(R.id.txt5)
    TextView mTxt5;

    @BindView(R.id.txt_repaytype)
    TextView mRepaytype;

    @BindView(R.id.txt_numdayss)
    TextView mNumdays;

    @BindView(R.id.txt_date1)
    TextView mData1;

    @BindView(R.id.txt_data2)
    TextView mData2;

    private String older_id, days, overdue_days, repay_amount, repay_end_time,loan_latefee,loan_capital;

    PayMoneyAdapter mAdapter;
    private BackMoneyDetailsBean bean;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case CommonValue.REQUEST_CODE1:
                    if ("SUCCESS".equals(JsonUtils.getInstance().getJsonObject(msg.obj.toString(), "result_pay"))) {
                        finish();
                    }
                    break;
            }
        }
    };

    @Override
    protected int getLayout() {
        return R.layout.activity_backmoneyone;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setStatusBarColor(R.color.balck);
        setToolBar("还款详情");
        older_id = getIntent().getStringExtra("repay_no");
        days = getIntent().getStringExtra("pay_days");
        overdue_days = getIntent().getStringExtra("overdue_days");
        repay_amount = getIntent().getStringExtra("repay_amount");
        repay_end_time = getIntent().getStringExtra("repay_end_time");
        loan_latefee=getIntent().getStringExtra("loan_latefee");
        loan_capital=getIntent().getStringExtra("loan_capital");
        bean = (BackMoneyDetailsBean) getIntent().getExtras().getSerializable("backMoneyBean");
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mOrder.setText("订单号：" + older_id);

        //剩余天数--借款截止日期-当前日期
        if (overdue_days.equals("0")){
            mRepaytype.setText("剩余天数");
            mNumdays.setText("" + days);
        } else if (days.equals("0")) {
            mRepaytype.setText("逾期天数");
            mNumdays.setText("" + overdue_days);
        }
        mTxt1.setText("日");
        mTxt2.setText("天");
        mTxt3.setText("应还金额(元)");
        mTxt4.setText("应还本金");
        mTxt5.setText("应还滞纳金");
        mLatefee.setText(mLatefee+"元");
        mData1.setText(TimeUtils.getStringnumber(repay_end_time + ""));
        mData2.setText("还款日期：" + TimeUtils.getStringData(repay_end_time + ""));
        mShouldrepaymoney.setText(repay_amount + "");
        mMoney.setText(loan_capital + "元");
        mLatefee.setText(loan_latefee+"元");

    }

    @Override
    protected void initDate() {
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case CommonValue.REQUEST_CODE1:
                    CouponInfo info = (CouponInfo) data.getExtras().getSerializable("bean");
//                    tv_free.setText("-" + info.getCoupon_value());
//                    tv_free.setTag(info.getCoupon_id());
                    break;
            }
        }
    }

    @OnClick({R.id.btn_finish, R.id.btn_ok})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_finish:
//                ActivityManagerUtils.getInstance().finishActivityclass(BackMoneyActivity.class);
                finish();
                break;
            case R.id.btn_ok:
                HttpUtils.getInstance().post(GetJsonUtils.getInstance().getRequest(HttpURL.REPAY_CONFIRM, mContext,
                        new String[]{"accessid", "coupon_id", "repay_channel", "repay_no", "return_url"},
                        new String[]{SPUtils.getAccessid(mContext), "", 2 + "", older_id, ""}), mContext, null, CommonValue.REQUEST_CODE1, this);
                break;
        }
    }

    @Override
    public void onSuccess(String content, Info info, int requestCode) {
        switch (requestCode) {
            case CommonValue.REQUEST_CODE1:
                new MobileSecurePayer().payRepayment(content, handler, CommonValue.REQUEST_CODE1, this, false);
                rl_result.setVisibility(View.VISIBLE);
                break;
        }
    }

    @Override
    public void onCacheSuccess(String content, Info info, int requestCode) {

    }

    @Override
    public void onError(Info info, int requestCode) {
        showToast(info.getMsg());
    }
}
